
(function($) {
	
	$.fn.textWave = function(options) {
		
		tw_seed = 0;
		tw_letters = 0;
		
		var settings = {
			'magnitude'	: 10,
			'speed' 		: 0.2,
			'offset'		: 0.4,
			'refresh'	: 30,
			'bounce'		: false
		};
		
		return this.each(function() {
			
			waveObj = $(this);
			
			if(options) { 
				$.extend(settings, options);
			}
			
			tmp = waveObj.text();
			tmpOut = "";
			tw_letters = tmp.length;
			for(count = 0; count < tmp.length; count++) {
				tmpOut+= '<span class="letter_'+count+'">'+tmp.charAt(count)+'</span>';
			}
			waveObj.html(tmpOut);
			
			setInterval("tw_loop()", settings.refresh);
		
			tw_loop = function() {
				
				tw_seed+= settings.speed;
				
				for(count = 0; count < tw_letters; count++) {
					seed = Math.sin(((tw_seed + (settings.offset * count)) % (Math.PI * 2)) - (Math.PI / 2));
					if(settings.bounce && seed > 0) seed*= -1;
					waveObj.children(".letter_" + count).css("position","relative");
					waveObj.children(".letter_" + count).css("top",(settings.magnitude * seed)+"px");
				}
				
			};
		});
	};
	
})(jQuery);

jQuery(window).load(function() {
	var Hover = $('.Hover');
	var Current = $('.current');

	var CurrentLeft = Current.position().left;
	var CurrentWidth = Current.parent().width();
	var UlLeft = $('.NavList').position().left;	
	Hover.css({'left': CurrentLeft + UlLeft - 8});
	Hover.find('.ItemSpan').css('width', CurrentWidth );
})
jQuery(document).ready(function($) {
	

	if($(".Slogan.vi").length > 0) {
		$('.Slogan').html("Môi trường xanh và thân thiện của bạn")
	}
	else {
		$('.Slogan').html('Your green and friendly environment')
	}
	$('.Slogan').textWave({
		'magnitude'	: 7,
		'speed': 0.1
	});
	var Hover = $('.Hover');
	var Current = $('.current');
		$('.Nav li a').hover(function() {
				var ThisLeft = $(this).position().left;
				var ThisWidth = $(this).parent().width();
				var UlLeft = $('.NavList').position().left;	
				Hover.stop(true, false)
				.animate({'left': ThisLeft + UlLeft - 8},{duration: 'fast', easing: 'swing', queue: false})
				.find('.ItemSpan').animate({'width':ThisWidth + 'px'},{duration:  100, easing: 'swing', queue: false});
				if(!$(this).hasClass('current')) {
					$('.current').addClass('currentWhite');
				}
			}, function() {
				var CurrentLeft = Current.parent().position().left;
				var CurrentWidth = Current.parent().width();
				var UlLeft = $('.NavList').position().left;	
				Hover.stop(true, false)
				.animate({'left': CurrentLeft + UlLeft - 8},{duration: 300, easing: 'swing', queue: false})
				.find('.ItemSpan').animate({'width':CurrentWidth + 'px'},  {duration: 100, easing: 'swing', queue: false});
				$('.current').removeClass('currentWhite');
	});

	$('.Language').hover(function() {
		var hHeight = $(this).find('h2').height();
		var thisHeight = $(this).height();
		var top = (thisHeight-hHeight) + 5;

		$(this).animate({'top': '-=' + top},{duration:200, easing:'swing'});
	}, function() {
		var hHeight = $(this).find('h2').height();
		var thisHeight = $(this).height();
		var top = (thisHeight-hHeight) + 5;

		$(this).animate({'top': '+=' + top},{duration:200, easing:'swing'});
	});

	setInterval(function() {
		$('.Language h2').html(function(i, oldHTML) {
			if(oldHTML == "Language") {
				return "Ngôn ngữ";
			} else {
				return "Language";
			}
		});

	}, 5000);

});
